package thread;

class MyBlockingQueue{
    private String[] elems = null;
    //[head,tail)
    //head 位置指向的是第一个元素，tail 指向的是最后一个元素的下一个元素
    private int head = 0;
    private int tail = 0;
    private int size = 0;



    public MyBlockingQueue(int capacity) {
        elems = new String[capacity];
    }
    void put(String elem){
        if(size >= elems.length){
            //队列满了，后续再来处理阻塞的情况
            return;

        }
        elems[tail] = elem;
        tail++;
        if (tail >= elems.length){
            //到达末尾，就回到开头
            tail = 0;
        }
        size++;
    }

    String take(){
        if (size == 0){
            //队列为空 ，后续再处理阻塞情况
            return null;
        }
        String result = elems[head];
        head++;
        if (head >= elems.length){
            head = 0;
        }
        size--;
        return result;
    }

}

public class Demo31 {
    public static void main(String[] args) {

    }
}
